package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.R;
import com.swgk.core.util.PickSelectEntity;

import java.util.List;

public class CustomTabLayout extends TabLayout implements TabLayout.OnTabSelectedListener {

    private int defaultTxtColor;
    private int selectTxtColor;
    private int defaultTxtSize;
    private int selectTxtSize;

    public CustomTabLayout(@NonNull Context context) {
        this(context, null);
    }

    public CustomTabLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, null, 0);
    }

    public CustomTabLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setTabStyle(int defaultTxtColor, int selectTxtColor, int defaultTxtSize, int selectTxtSize) {
        this.defaultTxtColor = defaultTxtColor;
        this.selectTxtColor = selectTxtColor;
        this.defaultTxtSize = defaultTxtSize;
        this.selectTxtSize = selectTxtSize;
    }

    public void setTabMenu(List<PickSelectEntity> data) {
        for (int i = 0; i < data.size(); i++) {
            TextView tv = getTabAt(i).getCustomView().findViewById(R.id.tv_tab_item);
            tv.setTextColor(defaultTxtColor);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, defaultTxtSize);
            tv.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tv.setText(data.get(i).getProjectscale());
        }
        updateViewLayout(getTabAt(getSelectedTabPosition()), true);
    }

    @Override
    public void onTabSelected(Tab tab) {
        updateViewLayout(tab, true);
        if (null != listener)
            listener.onTabSelected(tab);
    }

    @Override
    public void onTabUnselected(Tab tab) {
        updateViewLayout(tab, false);
        if (null != listener)
            listener.onTabUnselected(tab);
    }

    @Override
    public void onTabReselected(Tab tab) {

    }

    private void updateViewLayout(TabLayout.Tab tab, boolean isSelect) {
        if (isSelect) {
            TextView tv = tab.getCustomView().findViewById(R.id.tv_tab_item);
            tv.setTextColor(selectTxtColor);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, selectTxtSize);
            tv.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//加粗
            tv.invalidate();
        } else {
            TextView tv = tab.getCustomView().findViewById(R.id.tv_tab_item);
            tv.setTextColor(defaultTxtColor);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, defaultTxtSize);
            tv.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));//默认
            tv.invalidate();
        }
    }

    public interface OnTabSelectedListener {
        void onTabSelected(Tab tab);

        void onTabUnselected(Tab tab);
    }

    private OnTabSelectedListener listener;

    public void setListener(OnTabSelectedListener listener) {
        this.listener = listener;
    }
}
